package com.ict.web.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ict.common.utils.SecurityUtils;
import com.ict.common.utils.StringUtils;
import com.ict.common.utils.bean.BeanUtils;
import com.ict.common.utils.uuid.IdUtils;
import com.ict.web.constant.ProjectStatusEnum;
import com.ict.web.controller.file.domain.YzqFile;
import com.ict.web.controller.file.service.IYzqFileService;
import com.ict.web.domain.Approval;
import com.ict.web.domain.ProjectManagement;
import com.ict.web.domain.ReviewTask;
import com.ict.web.dto.AnnualDeclarationAddDto;
import com.ict.web.dto.FinanceAddDto;
import com.ict.web.dto.FinanceEditDto;
import com.ict.web.service.IApprovalService;
import com.ict.web.service.IProjectManagementService;
import com.ict.web.service.IReviewTaskService;
import com.ict.web.vo.FinanceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ict.web.mapper.FinanceMapper;
import com.ict.web.domain.Finance;
import com.ict.web.service.IFinanceService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 发改/财政局审批结果备案--刘杰Service业务层处理
 *
 * @author ruoyi
 * @date 2022-03-01
 */
@Service
public class FinanceServiceImpl extends ServiceImpl<FinanceMapper,Finance> implements IFinanceService
{
    @Autowired
    private FinanceMapper financeMapper;
    @Autowired
    private IProjectManagementService projectManagementService;
    @Autowired
    private IApprovalService approvalService;
    @Autowired
    private IReviewTaskService reviewTaskService;
    @Resource
    private IYzqFileService fileService;
    /**
     * 查询发改/财政局审批结果备案--刘杰
     *
     * @param financeId 发改/财政局审批结果备案--刘杰主键
     * @return 发改/财政局审批结果备案--刘杰
     */
    @Override
    public FinanceVo selectFinanceByFinanceId(Long financeId)
    {
        Finance finance = financeMapper.selectFinanceByFinanceId(financeId);
        FinanceVo financeVo = new FinanceVo();
        BeanUtils.copyProperties(finance,financeVo);
        YzqFile  approvalAnnexFile= fileService.getById(finance.getApprovalAnnexId());
        YzqFile  approvalFinanceFile= fileService.getById(finance.getApprovalFinanceId());
        if(StringUtils.isNotNull(approvalAnnexFile)){
            financeVo.setApprovalAnnexFile(approvalAnnexFile);
        }
        if(StringUtils.isNotNull(approvalFinanceFile)){
            financeVo.setApprovalFinanceFile(approvalFinanceFile);
        }
        return financeVo;
    }

    /**
     * 查询发改/财政局审批结果备案--刘杰列表
     *
     * @param finance 发改/财政局审批结果备案--刘杰
     * @return 发改/财政局审批结果备案--刘杰
     */
    @Override
    public List<Finance> selectFinanceList(Finance finance)
    {
        return financeMapper.selectFinanceList(finance);
    }

    /**
     * 新增发改/财政局审批结果备案--刘杰
     *
     * @param dto 发改/财政局审批结果备案--刘杰
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertFinance(FinanceAddDto dto)
    {
        Finance finance = new Finance();
        List<YzqFile> fileList = FinanceAddDto.of(finance,dto);
        financeMapper.insert(finance);
        //同步文件对象
        fileService.batchInsertFile(fileList);
        Assert.isTrue(StringUtils.isNotNull(dto.getProId()),"项目id不能为空");
        ProjectManagement projectManagement = projectManagementService.getById(dto.getProId());
        ReviewTask reviewTask = reviewTaskService.getById(projectManagement.getReviewTaskId());
        Assert.isTrue(StringUtils.isNotNull(reviewTask),"评审任务不能为空");
        Approval approval = approvalService.getApproval(projectManagement.getProId(), projectManagement.getIsState(),null,null);
        approval.setApprovalNode(ProjectStatusEnum.FINANCIAL_APPROVAL_AND_FILING_HAS_BEEN_PASSED.get().getNode());
        approval.setApprovalStage(ProjectStatusEnum.FINANCIAL_APPROVAL_AND_FILING_HAS_BEEN_PASSED.get().getStage());
        approval.setApprovalOperator(SecurityUtils.getNickName());
        approval.setEndTime(LocalDateTime.now());
        approval.setApprovalStatus(ProjectStatusEnum.FINANCIAL_APPROVAL_AND_FILING_HAS_BEEN_PASSED.get().getStatus());
        projectManagement.setIsState(ProjectStatusEnum.CONTRACT_FILING_TO_BE_SUBMITTED.get().getStatus());
        projectManagement.setNode(ProjectStatusEnum.CONTRACT_FILING_TO_BE_SUBMITTED.get().getNode());
        projectManagement.setStag(ProjectStatusEnum.CONTRACT_FILING_TO_BE_SUBMITTED.get().getStage());
        projectManagement.setFinanceId(finance.getFinanceId());
        //新增项目发改/财政审批备案进程
        approvalService.save(new Approval(IdUtils.snowId(),projectManagement.getProId(),ProjectStatusEnum.CONTRACT_FILING_TO_BE_SUBMITTED.get().getStage(),
                ProjectStatusEnum.CONTRACT_FILING_TO_BE_SUBMITTED.get().getNode(), LocalDateTime.now(),null, null,ProjectStatusEnum.CONTRACT_FILING_TO_BE_SUBMITTED.get().getStatus(),
                "/",null, null,null, LocalDateTime.now(),Approval.APPROVAL_TYPE_PROJECT_MANAGE));
        reviewTask.setNode(ProjectStatusEnum.CONTRACT_FILING_TO_BE_SUBMITTED.get().getNode());
        reviewTask.setNodeStatus(ProjectStatusEnum.CONTRACT_FILING_TO_BE_SUBMITTED.get().getStatus());
        projectManagementService.updateById(projectManagement);
        reviewTaskService.updateById(reviewTask);
        approvalService.updateById(approval);
        //同步项目数据到项目信息表中
        approvalService.synchronizationProjectInfo(projectManagement);
        return finance.getFinanceId();
    }

    /**
     * 修改发改/财政局审批结果备案--刘杰
     *
     * @param financeId 发改/财政局审批结果备案--刘杰
     * @return 结果
     */
    @Override
    public int updateFinance(FinanceAddDto dto)
    {
        ProjectManagement projectManagement = projectManagementService.getById(dto.getProId());
        Assert.isTrue(StringUtils.isNotNull(projectManagement),"未找到项目信息");
        Finance bean = financeMapper.selectById(projectManagement.getFinanceId());
        Assert.isTrue(StringUtils.isNotNull(bean),"未找到发改/财政局审批结果备案");
        YzqFile fileFinance = fileService.getById(bean.getApprovalFinanceId());
        if(StringUtils.isNotNull(fileFinance)){
            BeanUtils.copyProperties(dto.getApprovalFinance(),fileFinance);
            fileService.updateById(fileFinance);
        }
        YzqFile fileAnnex = fileService.getById(bean.getApprovalAnnexId());
        Assert.isTrue(StringUtils.isNotNull(fileAnnex),"未找初设及概算方案批复附件");
        BeanUtils.copyProperties(dto.getApprovalAnnex(),fileAnnex);
        fileService.updateById(fileAnnex);
        return financeMapper.updateById(bean);
    }

    /**
     * 删除发改/财政局审批结果备案--刘杰信息
     *
     * @param financeId 发改/财政局审批结果备案--刘杰主键
     * @return 结果
     */
    @Override
    public int deleteFinanceByFinanceId(Long financeId)
    {
        Finance bean = financeMapper.selectById(financeId);
        bean.setDelFlag("1");
        return financeMapper.updateById(bean);
    }
}
